package com.common.frame.utils


import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.content.res.XmlResourceParser
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import java.io.InputStream

object Res {

    private const val MAX_SIZE = 8 * 1012 * 1024
    private const val THUMBNAIL_MAX_SIZE = 512 * 1024



    fun getString(id: Int): String {
        return Utils.app.resources.getString(id)
    }

    fun getColor(id: Int): Int {
        return Utils.app.resources.getColor(id)
    }

    fun getString(id: Int, vararg formatArgs: Any?): String {
        return Utils.app.resources.getString(id, *formatArgs)
    }

    fun getDrawable(id: Int): Drawable {
        return Utils.app.resources.getDrawable(id)
    }

    fun getDimen(dimenId: Int): Float {
        return Utils.app.resources.getDimension(dimenId)
    }
    fun getIdentifier(name:String , defType:String  ): Int {
        return Utils.app.resources.getIdentifier(name,defType, Utils.app.packageName)
    }

    fun getStringArray(id: Int): Array<String> {
        return Utils.app.resources.getStringArray(id)
    }

    fun getDimensionPixelSize(id: Int): Int {
        return Utils.app.resources.getDimensionPixelSize(id)
    }

    fun openRawResource(id: Int): InputStream {
        return Utils.app.resources.openRawResource(id)
    }

    fun getXml(id: Int): XmlResourceParser {
        return Utils.app.resources.getXml(id)
    }
    fun obtainTypedArray(id: Int): TypedArray {
        return Utils.app.resources.obtainTypedArray(id)
    }
    fun resToDrawable(resId: Int, isThumbnail: Boolean): Drawable{
        val bitmap = BitmapFactory.decodeResource(Utils.app.resources, resId)
        val size = bitmap.byteCount
        val options = BitmapFactory.Options()
        if (isThumbnail) {
            options.inSampleSize = getSampleSize(size, THUMBNAIL_MAX_SIZE)
        } else {
            options.inSampleSize = getSampleSize(size, MAX_SIZE)
        }
        return BitmapDrawable(
            Utils.app.resources,
            BitmapFactory.decodeResource(Utils.app.resources, resId, options)
        )
    }


    fun fileToDrawable(path: String?, isThumbnail: Boolean): Drawable {
        val bitmap = BitmapFactory.decodeFile(path)
        val size = bitmap.byteCount
        val options = BitmapFactory.Options()
        if (isThumbnail) {
            options.inSampleSize = getSampleSize(size, THUMBNAIL_MAX_SIZE)
        } else {
            options.inSampleSize = getSampleSize(size, MAX_SIZE)
        }
        return BitmapDrawable(Utils.app.resources, BitmapFactory.decodeFile(path, options))
    }

    private fun getSampleSize(size: Int, maxSize: Int): Int {
        var sampleSize = 1
        var sizeBak = size
        while (sizeBak > maxSize) {
            sampleSize += 1
            sizeBak = size / (sampleSize * sampleSize)
        }
        return sampleSize
    }
}